#include "func.h"


void title1( DATA *data)
{
    printf("**********************************\n");
    printf("*  1 login  2 regist  3 exit     *\n");
    printf("**********************************\n");
    printf("Please choose:");
}
void title2( DATA *data)
{
    printf("**************%d:%s********************\n",data->num,data->name);
    printf("*   1.用户查找      2.历史查找         *\n");
    printf("*   3.修改          4,删除             *\n");
    printf("*   5.下班打卡      6、删除历史记录    *\n");
    printf("*   7.退出                             *\n");
    printf("****************************************\n");
    printf("Please choose:");
}
void title3( DATA *data)
{
    printf("**********************************\n");
    printf("*  1 查询所有使用记录              *\n");
    printf("*  2 查询某操作所有记录            *\n");
    printf("*  3 查询某用户的所有记录          *\n");
    printf("*  4 查询某用户的某操作所有记录    *\n");
    printf("**********************************\n");
    printf("Please choose:");
}

void log_in(int sockfd, DATA *data)
{
    
    int k;
    data->flags = 1;
    printf("请输入登入用户名：");
    fgets(data->name,sizeof(data->name),stdin);
    printf("请输入登入密码：");
    fgets(data->text1,sizeof(data->text1),stdin);
    data->name[strlen((data->name))-1] = '\0';
    data->text1[strlen((data->text1))-1] = '\0';
    if(send(sockfd,data,sizeof(DATA),0)<0)
    {
        printf("send error\n");
        exit(1);
    }   
    if(recv(sockfd,data,sizeof(DATA),0) < 0)
    {
        printf("recv error\n");
        exit(1);
    }
    if(data->text1[0] == 'n')
    {
       printf("用户名密码错误 请重新输入\n");
        // bzero(client_name,sizeof(client_name));
        // sprintf(client_name,data->name);
    }
    if(data->text1[0]=='y'&& data->num == 0)
    {
        printf("打卡成功\n");
    }
    text(data);  
    client_num = data->num;
    printf("%d\n",client_num);
}

void log_out(int sockfd, DATA *data)
{
    data->flags = 1;
    data->num = login_num;
    if(send(sockfd,data,sizeof(DATA),0)<0)
    {
        printf("send error\n");
        exit(1);
    }  
}

void regist(int sockfd, DATA *data)
{
    int k;
    data->flags = 2;
    while (1)
    {
        printf("请输入用户类型{1(管理员))：0（普通用户）}\n");
        scanf("%d",&k);
        getchar();
        if(k == 0)
        {
            data->num =0;
            break;
        }
        else if(k == 1)
        {
            printf("请输入密码\n");
            scanf("%d",&k);
            getchar();
            if(k == 95)
            {
                printf("密码正确\n");
                data->num = 95;
                break;
            }
            else
            {
                printf("密码错误\n");
            }
        }
        else
        {
            printf("您输入有误请重新输入\n");
        }
    }
    printf("请输入登入用户名：");
    fgets(data->name,sizeof(data->name),stdin);
    printf("请输入登入密码：");
    fgets(data->text3,sizeof(data->text3),stdin);
    printf("请输入职位：");
    fgets(data->text1,sizeof(data->text1),stdin);
    printf("请输入薪资：");
    fgets(data->text2,sizeof(data->text2),stdin);
    data->name[strlen((data->name))-1] = '\0';
    data->text1[strlen((data->text1))-1] = '\0';
    data->text2[strlen((data->text2))-1] = '\0';
    data->text3[strlen((data->text3))-1] = '\0';
    if(send(sockfd,data,sizeof(DATA),0)<0)
    {
        printf("send error\n");
        exit(1);
    }
    if(recv(sockfd,data,sizeof(DATA),0) < 0)
    {
        printf("recv error\n");
        exit(1);
    }
    if(data->text1[0] == 'n')
    {
        printf("用户已经存在\n");
    }
    if(data->text1[0] == 'y')
    {
        printf("创建成功\n");
    }
    if(data->num == 95)
    {
        data->num = 1;
    }
}

void delete(int sockfd, DATA *data)
{
    data->flags = 6;
    if(data->num == 0)
    {
        printf("您的权限不够\n");
        return;
    }
    if(data->num == 1)
    {
        printf("请输入您要删除的用户名:");
        fgets(data->text1,sizeof(data->text1),stdin);
        data->text1[strlen((data->text1))-1] = '\0';
        if(send(sockfd,data,sizeof(DATA),0)<0)
        {
            printf("delete send error\n");
            return;
        }
        if(recv(sockfd,data,sizeof(DATA),0)<0)
        {
            printf("delete recv error\n");
            exit(1);
        }
        if(data->text1[0]=='y')
        {
            printf("删除成功\n");
        }
        else
        {
            printf("删除失败\n");
        }

    }
}

void history_find(int sockfd, DATA *data)
{
    int n,a = 0;
    data->flags = 4;  
    bzero(client_name,sizeof(client_name));
    strcpy(client_name,data->name);
    int client_num = data->num;
    printf("%s     , %d    \n",client_name,data->num);
    if(data->num == 1)
    {
            title3(data);
            scanf("%d",&n);
            getchar();
            while(1)
            {
                switch (n)
                {
                case 1:
                    data->num = 1;
                    break;
                case 2:
                    data->num = 2;
                    printf("请输入您要查询的操作\n(login(登录\\打卡)、logout(下班打卡)、change(修改)、look(查询)、delete(删除))：");
                    fgets(data->text1,sizeof(data->text1),stdin);
                    data->text1[strlen((data->text1))-1] = '\0';
                    break;
                case 4:
                    data->num = 3;
                    printf("请输入您要查询的用户：");
                    fgets(data->text2,sizeof(data->text2),stdin);
                    printf("请输入您要查询的操作\n(login(登录\\打卡)、logout(下班打卡)、change(修改)、look(查询)、delete(删除))：");
                    fgets(data->text1,sizeof(data->text1),stdin);
                    data->text1[strlen((data->text1))-1] = '\0';
                    data->text2[strlen((data->text2))-1] = '\0';
                    break;
                case 3:
                    data->num = 4;  
                    printf("请输入您要查询的用户：");
                    fgets(data->text2,sizeof(data->text2),stdin);
                    data->text2[strlen((data->text2))-1] = '\0';
                    
                    break;
                default:
                    break;
                }
                if((strcmp(data->text1,"all")==0)||(strcmp(data->text1,"login")==0)||(strcmp(data->text1,"logout")==0)||(strcmp(data->text1,"change")==0)||(strcmp(data->text1,"look")==0)||(strcmp(data->text1,"delete")==0))
                {
                    break;
                }
                else if(data->num == 1||data->num == 4)
                {
                    break;
                }
                else
                {
                    printf("您输入的有误\n");
                }
            }
            if(send(sockfd,data,sizeof(DATA),0)<0)
            {
                printf("send error\n");
                exit(1);
            }
            while (1)
            {
                if(recv(sockfd,data,sizeof(DATA),0)<0)
                {
                    printf("history_find recv error\n");
                    exit(1);
                }
                if(data->flags != 0)
                {
                    text(data);
                }
                if(data->flags == 0)
                {
                    if(a == 0)
                    {
                        printf("您输入的有误请重新输入");
                    }
                    break;
                }
                strcpy(data->text1,data->text3);
                if(send(sockfd,data,sizeof(DATA),0)<0)
                {
                    printf("history_find recv error\n");
                    exit(1);
                }
                a = 1;
            }
    }
    else
    {
        printf("您的权限不够\n");
        return;
    }
    strcpy(data->name,client_name);
    data->num = client_num;
    client_num = 0;
    printf("%s     , %d    \n",client_name,data->num);
   
    
}

void delete_history(int sockfd, DATA *data)
{

    data->flags = 7;
    bzero(client_name,sizeof(client_name));
    strcpy(client_name,data->name);
    int client_num = data->num;
    if(send(sockfd,data,sizeof(DATA),0)<0)
    {
        printf("send error\n");
        exit(1);
    }
    if(recv(sockfd,data,sizeof(DATA),0)<0)
    {
        printf("send error\n");
        exit(1);
    }
    if(data->text1[0]=='n')
    {
        printf("操作失败\n");
    }
    else
    {
        printf("操作成功\n");
    }
    strcpy(data->name,client_name);
    data->num = client_num;
    client_num = 0;
}

void ues_find(int sockfd, DATA *data)
{
    data->flags = 3;
    bzero(client_name,sizeof(client_name));
    strcpy(client_name,data->name);
    int client_num = data->num;
    printf("%s     , %d    \n",client_name,data->num);
    if(send(sockfd,data,sizeof(DATA),0)<0)
    {
        printf("send error\n");
        exit(1);
    }
    while(1)
    {
        
        if(recv(sockfd,data,sizeof(DATA),0)<0)
        {
            printf("use_find recv error\n");
            exit(1);
        }
       if(data->flags != 0)
        {
            text(data);
        }
        if(data->flags == 0)
        {
            break;
        }
        strcpy(data->text1,data->text3);
        if(send(sockfd,data,sizeof(DATA),0)<0)
        {
            printf("history_find recv error\n");
            exit(1);
        }
        
    }
    strcpy(data->name,client_name);
    data->num = client_num;
    client_num = 0;
    printf("%s     , %d    \n",client_name,data->num);
}

void change(int sockfd, DATA *data)
{
    data->flags = 5;
    printf("请输入要修改的用户：");
    fgets(data->text1,sizeof(data->text1),stdin);
    printf("请输入要修改的变量：");
    fgets(data->text2,sizeof(data->text2),stdin);
    printf("请输入修改后的值为：");
    fgets(data->text3,sizeof(data->text3),stdin);

    data->text1[strlen((data->text1))-1] = '\0';
    data->text2[strlen((data->text2))-1] = '\0';
    data->text3[strlen((data->text3))-1] = '\0';
    text(data);
    if(send(sockfd,data,sizeof(DATA),0)<0)
    {
        printf("send error\n");
        exit(1);
    }
    if(recv(sockfd,data,sizeof(DATA),0) < 0)
    {
        printf("recv error\n");
        exit(1);
    }
    if(data->text1[0] == 'y')
    {
        printf("修改成功\n");
    }
    else if(data->text1[0]=='n')
    {
        printf("输入变量有误\n");
    }
    
}

void change_sp(int sockfd, DATA *data)
{
    data->flags = 5;
    printf("请输入要修改的用户：");
    fgets(data->text1,sizeof(data->text1),stdin);
    printf("请输入要修改的变量：");
    fgets(data->text2,sizeof(data->text2),stdin);
    printf("请输入修改后的值为：\n");
    fgets(data->text2,sizeof(data->text2),stdin);

    data->text1[strlen((data->text1))-1] = '\0';
    data->text2[strlen((data->text2))-1] = '\0';
    data->text3[strlen((data->text3))-1] = '\0';
    if(send(sockfd,data,sizeof(DATA),0)<0)
    {
        printf("send error\n");
        exit(1);
    }
    if(recv(sockfd,data,sizeof(DATA),0) < 0)
    {
        printf("recv error\n");
        exit(1);
    }
    if(data->text1[0] == 'y')
    {
        printf("修改成功\n");
    }
   
    
}

void text(DATA *data)
{
    printf("flage = %d, num = %d, name = %s,text1 = %s, text2 = %s,text3 = %s\n",
    data->flags,data->num,data->name,data->text1,data->text2,data->text3);
}
void text1(DATA *data)
{
    printf("name = %s,text1 = %s, text2 = %s,text3 = %s\n",
    data->name,data->text1,data->text2,data->text3);
}